using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
namespace Ketnoicsdl
{
    class Query
    {
       
        public void  loadtabale(string sql)
        {
            table = new DataTable();

            Adapter = new OleDbDataAdapter(sql, KetNoi.Connect);
            Commandbuilder = new OleDbCommandBuilder(Adapter);
            Adapter.Fill(table);
         
        }
        public void Insert(string tenban, params string[] cot)
        {
            try
            {
                string chuoi = "";
                foreach (string str in cot)
                {
                    if (cot.Length == 1 || str == cot[cot.Length - 1])
                    {
                        chuoi = chuoi + "'" + str + "'";

                    }
                    else
                    {
                        chuoi = chuoi + "'" + str + "'" + ",";
                    }
                }
                string sql = "insert into " + tenban + " values(" + chuoi + ")";
                OleDbCommand command = new OleDbCommand(sql,KetNoi.Connect);
                command.ExecuteNonQuery();
                
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message);

            }
        }
        public void Update(int row, params string[] giaitri)
        {
            //goi ham load table truoc
            DataRow dtrow = Table.Rows[row];
            int i = 0;
            for (int j = 0; j < table.Columns.Count; j++) 
            {
                MessageBox.Show(table.Columns[j].ToString());
                dtrow[table.Columns[j]] = giaitri[i].ToString();
                i++;
            }
            Adapter.Update(Table);
        }
        public void Update(string cactcot, int row, params string[] giaitri)
        {
            DataRow dtrow = Table.Rows[row];
            string[] tachcot = cactcot.Split(',');
            for (int i = 0; i < tachcot.Length; i++)
            {
                dtrow[tachcot[i]] = giaitri[i];
            }
            Adapter.Update(Table);
        }

        public void Delete( int row)
        {
            // goi ham load table truoc;
            Table.Rows[row].Delete();
            Adapter.Update(Table);

        }
        public DataTable Table
        {
            set
            {
                table = value;
            }
            get
            {
                return table;
            }
        }

        DataTable table;
        OleDbDataAdapter Adapter;
        OleDbCommandBuilder Commandbuilder;
        
        
    }
}
